package com.Serverce;

import com.DAO.Administorinfo;
import com.DAO.Menberinfo;
import com.Module.Boxnum;
import com.Module.MenberU;
import com.Util.Jpaneltest;
import com.Util.ModuleIte;
import com.Util.Window;
import com.Module.Administor;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

public class Administorview  {
    Administorview(){
        JFrame jf=new Window().window("管理员端",500,400,730,500);
        JPanel jPanel=new JPanel(new FlowLayout(0));
        CardLayout cardLayout=new CardLayout();
        JPanel jPanel1=new JPanel(cardLayout);

        CardLayout cardLayout1=new CardLayout(20,10);
        JPanel jp_modif=new JPanel(cardLayout1);
        JPanel jp_manag1=new JPanel();

        List<Boxnum> list_id_open=getlist("订单编号",15,10);
        Box box_id_=list_id_open.get(0).getBox();
        JTextField txt_id=list_id_open.get(0).getjTextField();
        List<Boxnum> list_name_open=getlist("客户姓名",15,10);
        Box box_name_=list_name_open.get(0).getBox();
        JTextField txt_name=list_name_open.get(0).getjTextField();
        List<Boxnum> list_number_open=getlist("身份证号",15,10);
        Box box_number_=list_number_open.get(0).getBox();
        JTextField txt_number=list_number_open.get(0).getjTextField();
        List<Boxnum> list_time_open=getlist("入住时间",15,10);
        Box box_time_=list_time_open.get(0).getBox();
        JTextField txt_time=list_time_open.get(0).getjTextField();
        List<Boxnum> list_roomid_open=getlist("房  间  号",15,10);
        Box box_roomid_=list_roomid_open.get(0).getBox();
        JTextField txt_roomid=list_roomid_open.get(0).getjTextField();

        Box box_=Box.createHorizontalBox();
        box_.add(box_id_);
        box_.add(Box.createHorizontalStrut(10));
        box_.add(box_name_);
        box_.add(Box.createHorizontalStrut(10));
        box_.add(box_number_);

        Box box_1=Box.createHorizontalBox();
        box_1.add(box_time_);
        box_.add(Box.createHorizontalStrut(10));
        box_1.add(box_roomid_);

        Box open_box_btn=Box.createHorizontalBox();
        JButton jb_open=new JButton("一键开房");
        jb_open.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id_=txt_id.getText();
                String name_=txt_name.getText();
                String numbercard=txt_number.getText();
                String time=txt_time.getText();
                String roomid_=txt_roomid.getText();
                MenberU menberU=new MenberU(Integer.parseInt(id_),name_,numbercard,time,Integer.parseInt(roomid_));
                Menberinfo menberinfo=new Menberinfo();
                Object obj=(Object) menberU;
                int ans_=menberinfo.update(obj);
                if (ans_ == 0) {
                    JOptionPane.showMessageDialog(null,"添加失败","失败",JOptionPane.WARNING_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null,"开房成功","成功",JOptionPane.OK_OPTION);
                }
            }
        });
        open_box_btn.add(jb_open);
        open_box_btn.add(Box.createHorizontalStrut(30));
        JButton jb_open_=new JButton("一键退房");
        jb_open_.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id__=txt_id.getText();
                String name__=txt_name.getText();
                String numbercard_=txt_number.getText() ;
                String time_=txt_time.getText();
                String roomid__=txt_roomid.getText();
                MenberU menberU_=new MenberU(Integer.parseInt(id__),name__,numbercard_,time_,Integer.parseInt(roomid__));
                Menberinfo menberinfo=new Menberinfo();
                Object obj=(Object) menberU_;
                int ans_=menberinfo.delete(obj);
                if (ans_ == 0) {
                    JOptionPane.showMessageDialog(null,"添加失败","添加成功",JOptionPane.WARNING_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null,"退房成功","成功",JOptionPane.OK_OPTION);
                }
            }
        });
        open_box_btn.add(jb_open_);
        open_box_btn.add(Box.createHorizontalStrut(30));
        JButton jb_time=new JButton("当前时间");
        jb_time.addActionListener(e -> txt_time.setText(new ModuleIte().gettime()));
        open_box_btn.add(jb_time);
        open_box_btn.add(Box.createHorizontalStrut(30));
        JButton jb_open1=new JButton("重置添加");
        jb_open1.addActionListener(e -> {
            txt_id.setText("");
            txt_name.setText("");
            txt_number.setText("");
            txt_time.setText("");
            txt_roomid.setText("");
        });
        open_box_btn.add(jb_open1);

        Box open_box=Box.createVerticalBox();
        open_box.add(box_);
        open_box.add(Box.createVerticalStrut(20));
        open_box.add(box_1);
        open_box.add(Box.createVerticalStrut(20));
        open_box.add(open_box_btn);
        jp_manag1.add(open_box);


        jp_manag1.add(open_box);

        jp_modif.add(jp_manag1,"1");

        JPanel jp=new JPanel();
        Administorinfo administorinfo=new Administorinfo();
        administorinfo.select();
        List<Administor> lis_adm=administorinfo.lis;
        Administor administor=lis_adm.get(0);
        Object[][] rowData = {{administor.getId(),administor.getName(),administor.getDepartment(),administor.getAddress(),administor.getYear()}};
        Object[] columnNames = {"编号","姓名","职务","家庭住址","工作年份"};
        DefaultTableModel defaultTableModel=new DefaultTableModel(rowData,columnNames);
        JTable jTable=new JTable();
        for (int i = 1; i < lis_adm.size(); i++) {
            Administor administor1=lis_adm.get(i);
            Object[] row = {administor1.getId(),administor1.getName(),administor1.getDepartment(),administor1.getAddress(),administor1.getYear()};
            defaultTableModel.addRow(row);
        }
        jTable.setModel(defaultTableModel);
        JScrollPane scrollPane = new JScrollPane(
                jTable,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
        ){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(700,450);
            }
        };
        jp.add(scrollPane);

        JPanel jp1=new JPanel();
        jp1.add(new Jpaneltest().getMenber());

        jPanel1.add(jp,"1");
        jPanel1.add(jp1,"2");
        jPanel1.add(new Jpaneltest().getUser(),"3");
        jPanel1.add(new Jpaneltest().getRoom(),"4");
        jPanel1.add(new Jpaneltest().getRoomtype(),"5");

        JMenuBar jMenuBar=new JMenuBar();
        JMenu menu=new JMenu("用户管理");
        JMenuItem item=new JMenuItem("查看用户");
        item.addActionListener(e -> cardLayout.show(jPanel1,"3"));
        JMenuItem item7=new JMenuItem("删除用户");
        item7.addActionListener(e -> new DeleteUser());
        menu.add(item);
        menu.add(item7);
        jMenuBar.add(menu);

        JMenuBar jMenuBar1=new JMenuBar();
        JMenu menu1=new JMenu("房间管理");
        JMenuItem item1=new JMenuItem("新建房间或类型");
        item1.addActionListener(e -> new RoomTypeview());
        JMenuItem item2=new JMenuItem("详细住房信息查看");
        item2.addActionListener(e -> cardLayout.show(jPanel1,"2"));
        JMenuItem item3=new JMenuItem("房间信息");
        item3.addActionListener(e -> cardLayout.show(jPanel1,"4"));
        JMenuItem item4=new JMenuItem("房间类型信息");
        item4.addActionListener(e -> cardLayout.show(jPanel1,"5"));
        menu1.add(item1);
        menu1.add(item2);
        menu1.add(item3);
        menu1.add(item4);
        jMenuBar1.add(menu1);

        JMenuBar jMenuBar2=new JMenuBar();
        JMenu menu2=new JMenu("个人信息");
        JMenuItem item5=new JMenuItem("查看管理员信息");
        item5.addActionListener(e -> cardLayout.show(jPanel1,"1"));
        JMenuItem item6=new JMenuItem("修改管理员密码");
        item6.addActionListener(e -> new Modify());

        menu2.add(item5);
        menu2.add(item6);
        jMenuBar2.add(menu2);


        JMenuBar jMenuBar4=new JMenuBar();
        JMenu menu4=new JMenu("其它");
        JMenuItem item9 =new JMenuItem("关于");
        item9.addActionListener(e -> new About());
        JMenuItem item10=new JMenuItem("退出");
        item10.addActionListener(e -> System.exit(0));
        JMenuItem item11=new JMenuItem("刷新");
        item11.addActionListener(e -> {new Administorview();jf.dispose();});
        JMenuItem item8=new JMenuItem("退出登录");
        item8.addActionListener(e -> {jf.dispose();new Login();});
        menu4.add(item10);
        menu4.add(item11);
        menu4.add(item8);
        menu4.add(item9);
        jMenuBar4.add(menu4);

        jPanel.add(jMenuBar);
        jPanel.add(jMenuBar1);
        jPanel.add(jMenuBar2);
        jPanel.add(jMenuBar4);
        cardLayout.show(jPanel1,"2");
        jf.add(jPanel,BorderLayout.NORTH);
        jf.add(jPanel1,BorderLayout.CENTER);
        jf.add(jp_modif,BorderLayout.SOUTH);
        jf.setVisible(true);
    }
    public static List<Boxnum> getlist(String title, int column, int width){
        Window window=new Window();
        List<Boxnum> list=window.HorizontalBox(title,column,width);
        return  list;
    }

    public static void main(String[] args) {
        new Administorview();
    }
}